<?php


global $_GPC, $_W;
$GLOBALS['frames'] = $this->getMainMenu2();

$where=" where uniacid=:uniacid and city_id=:city_id";
$data[':uniacid']=$_W['uniacid']; 
$data[':city_id']= $_COOKIE['account_id'];

if(isset($_GPC['keywords'])){
    $where.=" and title LIKE  concat('%', :name,'%') ";
    $data[':name']=$_GPC['keywords']; 
}
if(!empty($_GPC['time'])){
   $start=strtotime($_GPC['time']['start']);
   $end=strtotime($_GPC['time']['end']);
  $where.=" and UNIX_TIMESTAMP(time) >={$start} and UNIX_TIMESTAMP(time)<={$end}";
}

$pageindex = max(1, intval($_GPC['page']));
$pagesize=10;
$sql="select * from".tablename('zhtc_qrcode') ."".$where." ORDER BY id DESC";
$total=pdo_fetchcolumn("select count(*) from".tablename('zhtc_qrcode')."".$where,$data);
$select_sql =$sql." LIMIT " .($pageindex - 1) * $pagesize.",".$pagesize;
$list=pdo_fetchall($select_sql,$data);


function getinfo($array){
  $city_id = $_COOKIE['account_id'];
  $cityname = $_COOKIE['cityname'];
  foreach ($array as $a => $r) {
    
    if ($r['is_km'] == 1) {
      $kminfo = pdo_fetch("select * from ims_zhtc_video_card where city_id = ".$city_id." and id = ".$r['id']);
      $info['name'] = '自营';
      $info['typename'] = '视频卡';
      
    }else{

      $yhinfo = pdo_fetch("select * from ims_zhtc_coupons where is_show = 1 and cityname = '".$cityname."' and id = ".$r['id']);

      $info['name'] = pdo_get('zhtc_store',array('id'=>$yhinfo['store_id']))['store_name'];
      $info['typename'] = $yhinfo['cardtype'] == 0?'超级券':'优惠券';
    

    }
    $infos[] = $info;
    

  }

  return $infos;
} 
function getnum($array){


  $city_id = $_COOKIE['account_id'];
  $cityname = $_COOKIE['cityname'];
  foreach ($array as $a => $r) {
    
    if ($r['is_km'] == 1) {
      $kminfo = pdo_fetch("select * from ims_zhtc_video_card where city_id = ".$city_id." and id = ".$r['id']);
     
   

      $info['onnum'] = pdo_fetchcolumn("select count(*) from ims_zhtc_video_card_data where cid = ".$r['id']." and state = 1");
      $info['leftnum'] = pdo_fetchcolumn("select count(*) from ims_zhtc_video_card_data where cid = ".$r['id']." and state = 0");
    }else{

      $yhinfo = pdo_fetch("select * from ims_zhtc_coupons where is_show = 1 and cityname = '".$cityname."' and id = ".$r['id']);

      
     
      $info['onnum'] = pdo_fetchcolumn("select count(*) from ims_zhtc_usercoupons where coupons_id = ".$r['id']);
      $info['leftnum'] = $yhinfo['number'] - $info['onnum'];
      if ($info['leftnum']<0) {
        $info['leftnum'] = 0;
      }

    }
    $infos[] = $info;
    

  }
  return $infos;
}
$onnum = 0;
$leftnum = 0;
foreach ($list as $key => $value) {
  $array = getinfo(unserialize($value['cardids']));
  $num = getnum(unserialize($value['cardids']));
  $have = pdo_fetchcolumn("select count(*) from ims_zhtc_qrcode_data where cid = ".$value['id']);
  $lhave = pdo_fetchcolumn("select count(*) from ims_zhtc_qrcode_data where state = 0 and cid = ".$value['id']);
  $ohave = pdo_fetchcolumn("select count(*) from ims_zhtc_qrcode_data where state = 1 and cid = ".$value['id']);
  foreach ($num as $n) {
    $onnum += $n['onnum']; 
    $leftnum += $n['leftnum']; 
  }
  
  $list[$key]['cardlist'] = $array;
  $list[$key]['onnum'] = $onnum;
  if ($have != 0) {
     $list[$key]['have'] = 1;
     $list[$key]['lhave'] = $lhave;
     $list[$key]['ohave'] = $ohave;
  }
  $list[$key]['leftnum'] = $leftnum;
  unset($leftnum);
  unset($onnum);
 
}



$pager = pagination($total, $pageindex, $pagesize);
if($_GPC['op']=='delete'){
    $res=pdo_delete('zhtc_qrcode',array('id'=>$_GPC['id']));
    pdo_delete('zhtc_qrcode_data',array('cid'=>$_GPC['id']));
    if($res){
      message('删除成功！', $this->createWebUrl('qrcodelist'), 'success');
    }else{
      message('删除失败！','','error');
    }
}elseif($_GPC['op']=='zip'){
  // require_once("../addons/zh_tcwq/phpqrcode/phpqrcode.php");
  // $codelist = pdo_fetchall("select * from ims_zhtc_qrcode_data where cid =".$_GPC['cid']);
  $file_url = IA_ROOT."/addons/zh_tcwq/img/".$_GPC['cid']."/";
  // if (!file_exists($file_url)) {
  //   mkdir($file_url, 0777);
  // }
  // foreach ($codelist as $key => $value) {
  //       $outfile = $file_url.$value['code'].".png";
  //       if (!file_exists($outfile)) {
  //         $content = $_W['siteroot']."app/index.php?i=".$_W['uniacid']."&c=entry&do=getuserinfo&m=zh_tcwq&cardcode=".$value['code'];
          
  //         $level   = "L";
  //         $size    = 15;
  //         $margin  = 2;
  //         $save    = true;
  //         $res = \QRcode::png($content, $outfile, $level, $size, $margin, $save);
  //       }
  // }
 $filename = IA_ROOT."/addons/zh_tcwq/img/" . $_GPC['cid'].'_temp.zip';
 if (file_exists($file_url)) {
  $datalist = list_dir($file_url);

 
  if (!file_exists($filename)) {
    $zip = new ZipArchive();
    if ($zip->open($filename, ZIPARCHIVE::CREATE) !== TRUE) {
      exit('Create a failure');
    }
    foreach ($datalist as $val) {
      if (file_exists($val)) {
        $zip->addFile($val);
      }
    }
    $zip->close();
  }
  if (!file_exists($filename)) {
    exit('Cannot find the file');
  }
  header('Content-Type: application/zip;charset=utf8');
  header('Content-disposition: attachment; filename='.$_GPC['cid'].'_temp.zip');
  header('Content-Length: ' . filesize($filename));
  readfile($filename);
  //unlink($filenam);

 }else{
    if (file_exists($filename)) {
        header('Content-Type: application/zip;charset=utf8');
        header('Content-disposition: attachment; filename='.$_GPC['cid'].'_temp.zip');
        header('Content-Length: ' . filesize($filename));
      //  unlink($filenam);
        readfile($filename);
      
    }
 }

}elseif($_GPC['op']=='delzip'){

   $file_url = IA_ROOT."/addons/zh_tcwq/img/".$_GPC['cid']."/";
  deldir($file_url);

  $filename = IA_ROOT."/addons/zh_tcwq/img/" . $_GPC['cid'].'_temp.zip';
  unlink($filename);
}

function list_dir($dir)
  {
    $result = array();
    if (is_dir($dir)) {
      $file_dir = scandir($dir);
      foreach ($file_dir as $file) {
        if ($file == '.' || $file == '..') {
          continue;
        } else {
          if (is_dir($dir . $file)) {
            $result = array_merge($result, list_dir($dir . $file . '/'));
          } else {
            array_push($result, $dir . $file);
          }
        }
      }
    }
    return $result;
  }
function deldir($dir) {
   //先删除目录下的文件：
   $dh=opendir($dir);
   while ($file=readdir($dh)) {
      if($file!="." && $file!="..") {
         $fullpath=$dir."/".$file;
         if(!is_dir($fullpath)) {
            unlink($fullpath);
         } else {
            deldir($fullpath);
         }
      }
   }
 
   closedir($dh);
   //删除当前文件夹：
   if(rmdir($dir)) {
      return true;
   } else {
      return false;
   }
}
include $this->template('web/qrcodelist');